package com.swn.cm.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swn.cm.service.LoginService;
import com.swn.cm.service.MenuService;
import com.swn.common.constants.ICommonConstants;
import com.swn.common.util.JSONUtil;

@RequestMapping("/login")
@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	@Autowired
	private MenuService menuService;
	private static Logger logger = Logger
			.getLogger("LoginController.class");
	
	@RequestMapping(value = "/login")
	@ResponseBody
	public String login(String queryJsonStr,HttpSession session,HttpServletRequest request,HttpServletResponse response){
		String result="";
		try {
			@SuppressWarnings("unchecked")
			Map<String,Object> paramMap = JSONUtil.getMapFromJsonString(queryJsonStr);
			 //用户名
	         String name = (String) paramMap.get("login_user");
	         //密码
	         String passWord = (String) paramMap.get("login_pwd");
	         String remberPwd = (String) paramMap.get("remberPwd");
	         //记住用户名、密码功能(注意：cookie存放密码会存在安全隐患)
//	         String remFlag = request.getParameter("remFlag");
	         String loginInfo = name+","+passWord;
	         if("on".equals(remberPwd)){ //"on"表示用户勾选记住密码
	             Cookie userCookie=new Cookie("loginInfo",loginInfo); 
	             userCookie.setMaxAge(7*24*60*60);   //存活期为一个星期 30*24*60*60
	             userCookie.setPath("/");
	             response.addCookie(userCookie); 
	         }else {
	        	 Cookie newCookie=new Cookie("loginInfo",name+","+""); //假如要删除名称为username的Cookie
	        	 newCookie.setMaxAge(-1); //立即删除
	        	 newCookie.setPath("/"); //项目所有目录均有效
	        	 response.addCookie(newCookie);; //重新写入，将覆盖之前的
			}
	        //验证码信息
			String code=(String)session.getAttribute(ICommonConstants.CHECKCODE);
			if(!paramMap.get("code").equals(code)){
				return "errorcode";
			}
			String loginname = "";
			loginname=(String)paramMap.get("login_user");
			List<Map<String, Object>> emplist = loginService.queryEmp(loginname);
			if(emplist.isEmpty()){
				return "nameEmpty";
			}
			if(paramMap.get("login_pwd").equals(emplist.get(0).get("login_pwd"))){
				result="success";
				session.setAttribute(ICommonConstants.LOGIN_USER,emplist.get(0));
				session.setAttribute(ICommonConstants.LOGIN_MENUROLE,menuService.queryMenuByRoleID(emplist.get(0).get("role_id").toString()));
				session.setAttribute(ICommonConstants.LOGIN_USER_NAME,emplist.get(0).get("login_user"));
			}else{
				result="errorpwd";
			}
		} catch (Exception e) {
			logger.error(e);
		}
		
		return result;
	}
	
}
